var express = require('express');
var app = express();
var bodyParser = require('body-parser');//用于req.body获取值的
var redis = require('./redis')
var localDb = require('./db')


// 处理静态资源
app.use(express.static('public'))
// 处理参数
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

app.get('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "PUT,GET,POST,DELETE,OPTIONS");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-hEADERS", "Content-Type");
    next();
});

// 保存总的数据
global.db = {}

app.listen(8888, function () {
    console.log('访问```：127.0.0.1:8888');
});

app.get('/', function (req, res) {

    redis.initSet(1, 'db', JSON.stringify(localDb), 86400, function (err, result) {
        result === "OK" && err === null && console.log('~~加载数据成功~~')
        result !== "OK" && console.log('===加载数据失败===')
    })
    res.send('！！加载数据成功~~');
})

app.get('/get', function (req, res) {
    redis.get(function (e, r) { 
        // console.log('r222~~~', typeof r)
        res.send(JSON.stringify(r));
    })
}) 



